<?php


require ROOT_PATH.'/Helper/admin_abstract.php';

class Action extends admin_abstract
{
    function index()
    {
        $user_group_id = $this->info['user_group']['id'];
        if ($user_group_id == '1') {
            $data = _model('store_front')->getList('ORDER BY add_time DESC', new Pager(20));
        } elseif ($user_group_id > 2) { // editor   
            $data = _model('store_front')->getList('WHERE find_in_set('.$this->info['id'].',editor) ORDER BY add_time DESC', new Pager(20));
        } elseif ($user_group_id == '2') {
            $user_info = _model('user')->read(array('id'=>$this->info['id']));
            if (empty($user_info['tags'])) {
                $this->msg('You have no tag');
            }
            $user_tags = explode('|', $user_info['tags']);
            $store_front_data = _model('store_front')->getList("WHERE tags!=''");
            $in_set = array();
            foreach ($store_front_data as $val) {
                $val['tags'] = explode('|', $val['tags']);
                if (!array_diff($val['tags'], $user_tags)) {
                    $in_set[] = $val['id'];
                }
                // $tmp_res = array_intersect($user_tags, $val['tags']);
                // if (!empty($tmp_res)) {
                //     $in_set[] = $val['id'];
                // }
            }
            $in_set = array_unique($in_set);
            
            if (!empty($in_set)) {
                $data = _model('store_front')->getList('WHERE id IN('.implode(',', $in_set).') ORDER BY add_time DESC', new Pager(20));
            } else {
                $data = array('data'=>array());
            }
        }
        $this->view->assign($data);
        $this->view->display('admin/frontstore.html');
    }
    
    function add()
    {
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            $info = _POST('info', array());
            $sf_id = _POST('sf_id', 0);
            $info['user_id'] = $this->info['id'];
            $info['add_time'] = time();
            if (!$info['store_name'] || !$info['domain'] || !$info['logo']) {
                $this->msg('Lacking parameter');
            }
            if (is_array($info['editor'])) {
                $info['editor'] = implode(',', $info['editor']);
            }
            if ($sf_id) {
                _model('store_front')->update(array('id'=>$sf_id), $info);
            } else {
                _model('store_front')->create($info);
            }
            $this->msg('Operation was successful!','admin/store_front.html');
        } else {
            $editor = _model('user')->getList('WHERE user_group > 2');
            $this->view->assign('editor', $editor);
            $this->view->display('admin/add_frontstore.html');
        }
    }
    
    function edit()
    {
        

        
        $id = _GET('id', 0);
        if (!$id || !$info = _model('store_front')->read(array('id'=>$id))) {
            $this->msg('Lacking parameter');
        }
        
        if ($info['editor']) {
            $info['editor'] = explode(',', $info['editor']);
        } else {
            $info['editor'] = array();
        }
        
        $user_group_id = $this->info['user_group']['id'];
        if ($user_group_id > 2) {
            $this->msg('No permission');
        }
        // if ($user_group_id == '4') {
        //     if (!in_array($this->info['id'], $info['editor'])) {
        //         return false;
        //     }
        // }
        $editor = _model('user')->getList('WHERE user_group > 2');
        $this->view->assign('editor', $editor);
        $this->view->assign('info', $info);
        $this->view->display('admin/add_frontstore.html');
    }
    
    function delete()
    {
        $id = _GET('id', 0);
        if (!$id || !$info = _model('store_front')->read(array('id'=>$id))) {
            $this->msg('Lacking parameter');
        }
        
        if ($info['editor']) {
            $info['editor'] = explode(',', $info['editor']);
        } else {
            $info['editor'] = array();
        }
        
        $user_group_id = $this->info['user_group']['id'];
        if ($user_group_id > 2) {
            $this->msg('No permission');
        }
        // if ($user_group_id == '4') {
        //     if (!in_array($this->info['id'], $info['editor'])) {
        //         return false;
        //     }
        // }
        _model('store_front')->delete(array('id'=>$id));
        _model('user')->update(array('homepage'=>$info['domain']), array('homepage'=>'/'));
        
        $this->redirect();
    }
    
    function set_tags($sid)
    {
        if (!$s_info = _model('store_front')->read(array('id'=>$sid))) {
            //@todo check msg
            $this->msg('store front not exists');
        }

        if (Request::isPost()) {
            $tags = _POST('tags', array());
            $tags = array_filter($tags);
            _model('store_front')->update(array('id'=>$sid), array('tags' => implode('|', $tags)));
            $this->msg('Set tags was successful!', "admin/store_front/set_tags/$sid.html");
        } else {
            $tags = array();
            $data = _model('tags')->getList('ORDER BY view_order ASC');
            foreach ($data as $key => $val) {
                $tags[$val['id']] = $val;
            }
            $s_info['tags'] = array_filter(explode('|', $s_info['tags']));
            $this->view->assign('tags', $tags);
            $this->view->assign('info', $s_info);
            $this->view->display('admin/storefronts_set_tags.html');
        }

    }
    
    function set_marqueeads($sf_id)
    {
        if (!$info = _model('store_front')->read(array('id'=>$sf_id))) {
            $this->msg('store front not exists');
        }
        
        if (Request::isPost()) {
            $info = _POST('info', array());
            if (empty($info['title']) || empty($info['link']) || empty($info['pic']) || empty($info['store_id'])) {
                $this->msg('wrong request');
            }
            $info['add_time'] = time();
            _model('marquees')->create($info);

            $this->cache->ns('marquees')->delete();

            $this->redirect('admin/store_front/set_marqueeads/'.$info['store_id'].'.html');
        } else {
            $data = _model('marquees')->getList('WHERE store_id='.$sf_id.' ORDER BY add_time ASC');
            $this->view->assign('data', $data);
            $this->view->assign('info', $info);
            $this->view->display('admin/storefronts_set_marqueeads.html');
        }
    }
    
    
    function marquees_delete($id)
    {
        _model('marquees')->delete(array('id'=>$id));

        $this->cache->ns('marquees')->delete();

        $this->redirect();
    }

    function marquees_update($id)
    {
        $info = _POST('info', array());
        if (empty($info['title']) || empty($info['link']) || empty($info['pic'])) {
            $this->msg('wrong request');
        }
        _model('marquees')->update(array('id'=>$id), $info);

        $this->cache->ns('marquees')->delete();
        
        $this->redirect();
    }

    function set_companies($sf_id)
    {
        if (!$info = _model('store_front')->read(array('id'=>$sf_id))) {
            $this->msg('store front not exists');
        }
        if (Request::isPost()) {
            $companies = _POST('companies', array());
            $companies = array_filter($companies);
            _model('store_front')->update(array('id'=>$sf_id), array('companies' => implode(',', $companies)));
            $this->msg('Set companies was successful!', "admin/store_front/set_companies/$sf_id.html");
        } else {
            $data = _model('company')->getList('ORDER BY add_time DESC');
            $info['companies'] = array_filter(explode(',', $info['companies']));
            $this->view->assign('data', $data);
            $this->view->assign('info', $info);
            $this->view->display('admin/storefronts_set_companies.html');
        }
    }
    
    function set_assets($sf_id)
    {
        if (!$info = _model('store_front')->read(array('id'=>$sf_id))) {
            $this->msg('store front not exists');
        }
        
        if (Request::isPost()) {
            $asset = _POST('asset', array());
            $url = _POST('j_url');
            $asset = array_filter($asset);
            //处理
            $back_post = _POST('back_post');
            if (!empty($back_post)) {
                $asset = array_merge($asset, explode('|', $back_post));
            }
            //
            _model('store_front')->update(array('id'=>$sf_id), array('assets' => implode('|', $asset)));
            if ($url) {
                $this->redirect($url);
                //$this->msg('Set assets was successful!', $url);
            } else {
                $this->redirect();
                //$this->msg('Set assets was successful!', "admin/store_front/set_assets/$sf_id.html");
            }
        } else {
            $q = addslashes(_GET('q'));
            $tags = _GET('ids');
            $tags = array_filter(explode(',', $tags));
            
            $order = _GET('order');
            $order_key = _GET('order_key');
            $order_key_check = array('user_id');
            $order_check = array('asc', 'desc');
            if (!in_array($order, $order_check)) {
                $order = 'desc';
            }
            if (!in_array($order_key, $order_key_check)) {
                $order = 'add_time';
            }
            $order = $order_key.'|'.$order;
            
            // if (!$q) {
            //     $data = _model('assets')->getList("ORDER BY $order", new Pager(20));
            // } else {
            //     $data = _model('assets')->getList("WHERE title LIKE '%{$q}%' ORDER BY $order", new Pager(20));
            // }
            
            $data = _model('assets')->get_search_result($q, $tags, 20, $order, array(), 0);
            
            
            $tags = $this->get_tags();
            $this->view->assign('tags', $tags);
            $this->view->assign($data);
            $info['assets'] = array_filter(explode('|', $info['assets']));
            
            //处理
            $back_post = $info['assets'];
            foreach ($data['data'] as $key => $value) {
                if (in_array($value['id'], $info['assets'])) {
                    $k = array_search($value['id'], $info['assets']);
                    unset($back_post[$k]);
                }
            }
            $back_post = implode('|', $back_post);
            //
            $this->view->assign('back_post', $back_post);
            $this->view->assign('info', $info);
            $this->view->display('admin/storefronts_set_assets.html');   
        }
        
    }

}


?>